/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: deserializer.cpp
 *
 * Purpose: deserializer implementation for 'COM' Functional
 *
 * Developer:
 *   wen.gu , 2020-07-28
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "panda/sys/deserializer.h"

namespace panda
{
namespace sys
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define CHECK_LEFT_SIZE(val) ((mImpl->mPos + sizeof(val)) < mImpl->mSize)

#define SHIFT_READ(data, curPos, type, shiftBit) (type(data[curPos]) << shiftBit)

#define SWAP_READ16(data, curPos) (uint16_t(data[curPos]) | \
                                   SHIFT_READ(data, curPos + 1, uint16_t, 8))

#define SWAP_READ32(data, curPos) ((uint32_t(data[curPos])) | \
                                   SHIFT_READ(data, curPos + 1, uint32_t, 8)  | \
                                   SHIFT_READ(data, curPos + 2, uint32_t, 16) | \
                                   SHIFT_READ(data, curPos + 3, uint32_t, 24))

#define SWAP_READ64(data, curPos) ((uint64_t(data[curPos])) | \
                                   SHIFT_READ(data, curPos + 1, uint64_t, 8)  | \
                                   SHIFT_READ(data, curPos + 2, uint64_t, 16) | \
                                   SHIFT_READ(data, curPos + 3, uint64_t, 24) | \
                                   SHIFT_READ(data, curPos + 3, uint64_t, 32) | \
                                   SHIFT_READ(data, curPos + 3, uint64_t, 40) | \
                                   SHIFT_READ(data, curPos + 3, uint64_t, 48) | \
                                   SHIFT_READ(data, curPos + 3, uint64_t, 56))

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/


class Deserializer::impl
{
public:
    bool mChangeByteOrder = false;
    PayloadPtr mData = nullptr; /**< pointer to data buffer */
    uint32_t mPos = 0;      /**< offset position of parcel operation */
    uint32_t mSize = 0;       /**< the size of data buffer */
};

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
Deserializer::Deserializer(PayloadPtr data, uint32_t size, DataByteOrder byteOrder /*= panda::core::GetPlatformByteOrder(*/)
    :mImpl(new impl)
{
    mImpl->mData = data;
    mImpl->mSize = size;

    mImpl->mChangeByteOrder = (byteOrder != panda::core::GetPlatformByteOrder());
    /** todo something */
}

Deserializer::~Deserializer()
{
    /** todo something */
}

/** for deserialize */
bool Deserializer::read(bool& val)
{
    if (mImpl->mPos < mImpl->mSize)
    {
        val = bool(mImpl->mData.get()[mImpl->mPos++]);
        return true;
    }

    return false;
}

bool Deserializer::read(uint8_t& val)
{
    if (mImpl->mPos < mImpl->mSize)
    {
        val = mImpl->mData.get()[mImpl->mPos++];
        return true;
    }

    return false;
}

bool Deserializer::read(uint16_t& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            val = SWAP_READ16(data, curPos);

            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(uint32_t& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            val = SWAP_READ32(data, curPos);

            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(uint64_t& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            val = SWAP_READ64(data, curPos);
            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(int8_t& val)
{
    if (mImpl->mPos < mImpl->mSize)
    {
        val = mImpl->mData.get()[mImpl->mPos++];
        return true;
    }

    return false;
}

bool Deserializer::read(int16_t& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            val = SWAP_READ16(data, curPos);

            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(int32_t& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            val = SWAP_READ32(data, curPos);

            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(int64_t& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            val = SWAP_READ64(data, curPos);
            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(float& val) /** todo refine me?? if float bit changed in diffrent platform */
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            uint32_t tmp = SWAP_READ64(data, curPos);
            memcpy(&val, &tmp, sizeof(val));

            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(double& val)
{
    if (mImpl->mChangeByteOrder)
    {
        if (CHECK_LEFT_SIZE(val))
        {
            uint32_t curPos = mImpl->mPos;
            uint8_t* data = mImpl->mData.get();

            uint64_t tmp = SWAP_READ64(data, curPos);
            memcpy(&val, &tmp, sizeof(val));
            mImpl->mPos += sizeof(val);
            return true;
        }
        else
        {
            return false;
        }
    }

    return read((uint8_t*)&val, sizeof(val));
}

bool Deserializer::read(uint8_t* data, uint32_t size)
{
    if ((mImpl->mPos + size) < mImpl->mSize)
    {
        memcpy(data, mImpl->mData.get() + mImpl->mPos, size);
        mImpl->mPos += size;
        return true;
    }

    return false;
}

bool Deserializer::read(std::string& val)
{
    if (mImpl->mPos < mImpl->mSize)
    {
        uint32_t curPos = mImpl->mPos;
        uint8_t* data = mImpl->mData.get();
        bool ret = false;

        while (curPos < mImpl->mSize)
        {
            char tmp = data[curPos++];

            if (tmp)
            {
                val.append(1, tmp);
            }
            else
            {
                mImpl->mPos = curPos;
                ret = true;
                break;
            }
        }

        return ret;
    }

    return false;
}

} /** namespace sys */
} /** namespace panda */


